<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Symbol属性名的遍历</title>
</head>
<body>
    <h1>Symbol属性名的遍历</h1>

    <script>
        const obj = {};
        let a = Symbol('a');
        let b = Symbol('b');

        obj[a] = 'Hello';
        obj[b] = 'World';
        obj.c = 'normal';

        // Symbol属性不会出现在for...in、for...of循环中
        for (let i in obj) {
            console.log(i); // 只输出"c"
        }

        // Object.keys()也无法获取Symbol属性
        console.log(Object.keys(obj)); // ['c']

        // 使用Object.getOwnPropertySymbols()获取Symbol属性
        console.log(Object.getOwnPropertySymbols(obj)); // [Symbol(a), Symbol(b)]

        // Reflect.ownKeys()可以获取所有类型的键名
        console.log(Reflect.ownKeys(obj)); // [Symbol(a), Symbol(b), 'c']
    </script>
</body>
</html> 